package com.cherish.meeting.db.mapper;

import com.cherish.meeting.db.entity.MeetingRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2021-03-02
 */
public interface MeetingRecordMapper {


    @Select(
            "SELECT id, openid, room_area as roomArea, room_no as roomNo,meeting_date as meetingDate, start_time as startTime, end_time as endTime,title,status FROM `meeting_record` " +
                    "WHERE " +
                    "room_area = #{roomArea, jdbcType=VARCHAR} " +
                    "AND room_no = #{roomNo, jdbcType=VARCHAR} " +
                    "AND meeting_date = #{meetingDate, jdbcType=DATE} " +
                    "AND (start_time <= #{endTime, jdbcType=TIME} OR end_time >= #{startTime, jdbcType=TIME}) " +
                    "AND status = 1"
    )
    List<MeetingRecord> bookRoomCheck(MeetingRecord record);


    @Select(
            "SELECT r.id, r.openid, u.name as roomArea, r.meeting_date as meetingDate, r.start_time as startTime, r.end_time as endTime,r.title " +
                    "FROM `meeting_record` r left join user_info u on u.openid = r.openid " +
                    "WHERE " +
                    "r.room_area = #{roomArea, jdbcType=VARCHAR} " +
                    "AND r.room_no = #{roomNo, jdbcType=VARCHAR} " +
                    "AND (meeting_date > #{meetingDate, jdbcType=DATE} " +
                    "OR (meeting_date = #{meetingDate, jdbcType=DATE} AND end_time >= #{endTime, jdbcType=TIME} ))" +
                    "AND r.status = 1 " +
                    "order by r.meeting_date,r.start_time limit 20"
    )
    List<MeetingRecord> getMeetingByRoom(MeetingRecord record);

    @Select(
            "SELECT id, openid, room_area as roomArea, room_no as roomNo,meeting_date as meetingDate, start_time as startTime, end_time as endTime,title,status FROM `meeting_record` " +
                    "WHERE " +
                    "openid = #{openid, jdbcType=VARCHAR} " +
                    "AND (meeting_date > #{meetingDate, jdbcType=DATE} " +
                    "OR (meeting_date = #{meetingDate, jdbcType=DATE} AND end_time >= #{endTime, jdbcType=TIME} )) AND status = 1 " +
                    "order by meeting_date,start_time limit 20"
    )
    List<MeetingRecord> getMyMeeting(MeetingRecord record);

    @Select(
            "SELECT id, openid, room_area as roomArea, room_no as roomNo,meeting_date as meetingDate, start_time as startTime, end_time as endTime,title,status FROM `meeting_record` " +
                    "WHERE " +
                    "room_area = #{roomArea, jdbcType=VARCHAR} " +
                    "AND room_no = #{roomNo, jdbcType=VARCHAR} " +
                    "AND openid = #{openid, jdbcType=VARCHAR} " +
                    "AND (meeting_date > #{meetingDate, jdbcType=DATE} " +
                    "OR (meeting_date = #{meetingDate, jdbcType=DATE} AND end_time >= #{endTime, jdbcType=TIME} )) AND status = 1"
    )
    MeetingRecord getMyMeetingAtRoom(MeetingRecord record);

    /**
     * 写入
     *
     * @param record
     */
    @Insert(
            "insert into `meeting_record` " +
                    "(`openid`, `room_area`, `room_no`, `meeting_date`, `start_time`, `end_time`, `title`) " +
                    "values " +
                    "(#{openid,jdbcType=VARCHAR}, #{roomArea,jdbcType=VARCHAR}, #{roomNo,jdbcType=VARCHAR}, #{meetingDate,jdbcType=DATE}, #{startTime,jdbcType=TIME}, #{endTime,jdbcType=TIME}, #{title,jdbcType=VARCHAR})"
    )
    int insert(MeetingRecord record);

}
